using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using CYZHAPI.Common.DB;
using CYZHAPI.Entity;
using CYZHAPI.IRepository;
using Oracle.ManagedDataAccess.Client;

namespace CYZHAPI.Repository {
    public class ModisRep : ReponsitoryBase<ModisModel>, IModisRep {
        public ModisRep (IDapperContext dbContext) : base (dbContext) { }

        bool IModisRep.delete (long key) {
            return base.delete (key, "id", "MODIS_DATA_DIRECTORIES");
        }

        List<ModisModel> IModisRep.GetList (string sWhere) {
            List<ModisModel> reList = new List<ModisModel> ();
            DataSet ds = base.GetList ("MODIS_DATA_DIRECTORIES", sWhere);
            if (ds.Tables[0].Rows.Count > 0) {
                foreach (DataRow dr in ds.Tables[0].Rows) {
                    ModisModel objBean = new ModisModel () {
                        id = Convert.ToInt32 (dr["id"]),
                        dataformat = dr["dataformat"].ToString (),
                        rowcol = dr["rowcol"].ToString (),
                        filetime = dr["filetime"].ToString (),
                        days = Convert.ToInt32 (dr["days"]),
                        year = Convert.ToInt32 (dr["year"]),
                        filename = dr["filename"].ToString (),
                        createtime = Convert.ToDateTime (dr["createtime"]),
                        createby = dr["createby"].ToString (),
                        updatetime = Convert.ToDateTime (dr["updatetime"]),
                        updateby = dr["updateby"].ToString (),
                        mtype = dr["mtype"].ToString ()
                    };
                    reList.Add (objBean);
                    //foreach (DataColumn dc in ds.Tables[0].Columns) {}
                }
            } else {
                return null;
            }

            return reList;
        }
        List<ModisModel> IModisRep.GetListUninXZQH (string sWhere) {
            List<ModisModel> reList = new List<ModisModel> ();
            DataSet ds = base.GetList ("MODIS_DATA_DIRECTORIES a,MODIS_XZQH b", sWhere);
            if (ds.Tables[0].Rows.Count > 0) {
                foreach (DataRow dr in ds.Tables[0].Rows) {
                    ModisModel objBean = new ModisModel () {
                        id = Convert.ToInt32 (dr["id"]),
                        dataformat = dr["dataformat"].ToString (),
                        rowcol = dr["rowcol"].ToString (),
                        filetime = dr["filetime"].ToString (),
                        days = Convert.ToInt32 (dr["days"]),
                        year = Convert.ToInt32 (dr["year"]),
                        filename = dr["filename"].ToString (),
                        createtime = Convert.ToDateTime (dr["createtime"]),
                        createby = dr["createby"].ToString (),
                        updatetime = Convert.ToDateTime (dr["updatetime"]),
                        updateby = dr["updateby"].ToString (),
                        mtype = dr["mtype"].ToString ()
                    };
                    reList.Add (objBean);
                    //foreach (DataColumn dc in ds.Tables[0].Columns) {}
                }
            } else {
                return null;
            }

            return reList;
        }
         List<ModisResultsModel> IModisRep.GetMosisResultsList(string sWhere){
             List<ModisResultsModel> reList = new List<ModisResultsModel> ();
            DataSet ds = base.GetList ("ModisResults", sWhere);
            if (ds.Tables[0].Rows.Count > 0) {
                foreach (DataRow dr in ds.Tables[0].Rows) {
                    ModisResultsModel objBean = new ModisResultsModel () {
                        id = Convert.ToInt32 (dr["id"]),
                        layername = dr["layername"].ToString (),
                        scope = dr["scope"].ToString (),
                        filetime = dr["filetime"].ToString (),
                        filename = dr["filename"].ToString (),
                        algorithm= dr["algorithm"].ToString (),
                        days = dr["days"].ToString(),
                        year = dr["year"].ToString(),
                        createtime = Convert.ToDateTime (dr["createtime"]),
                        createby = dr["createby"].ToString (),
                        updatetime = Convert.ToDateTime (dr["updatetime"]),
                        updateby = dr["updateby"].ToString (),
                       
                    };
                    reList.Add (objBean);
                    
                }
            } else {
                return null;
            }

            return reList;
         }

        bool IModisRep.insert (string TableName, List<ModisModel> itemModel) {
            DataSet DataSetItem = new DataSet ();
            DataTable dt = new DataTable (TableName); //创建一个名为Table的DataTalbe

            dt.Columns.Add (new DataColumn ("id", typeof (int))); //为dt_dry表内建立Column
            dt.Columns.Add (new DataColumn ("dataformat", typeof (string)));
            dt.Columns.Add (new DataColumn ("rowcol", typeof (string)));
            dt.Columns.Add (new DataColumn ("filetime", typeof (string)));
            dt.Columns.Add (new DataColumn ("year", typeof (int)));
            dt.Columns.Add (new DataColumn ("days", typeof (int)));
            dt.Columns.Add (new DataColumn ("filename", typeof (string)));
            dt.Columns.Add (new DataColumn ("mtype", typeof (string)));
            dt.Columns.Add (new DataColumn ("createtime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("createby", typeof (string)));
            dt.Columns.Add (new DataColumn ("updatetime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("updateby", typeof (string)));

            foreach (ModisModel v in itemModel) {
                DataRow dr = dt.NewRow (); //注意这边创建dt的新行的方法。指定类型是DataRow而不是TableRow，然后不用new直接的用创建的DataTable下面的NewRow方法。
                dr["id"] = this.GetProblemXH ("MODIS_DATA_SEQUENCE");
                dr["dataformat"] = v.dataformat;
                dr["rowcol"] = v.rowcol;
                dr["filetime"] = v.filetime;
                dr["year"] = v.year;
                dr["days"] = v.days;
                dr["filename"] = v.filename;
                dr["mtype"] = v.mtype;
                dr["createtime"] = DateTime.Now.Date;
                dr["createby"] = v.createby;
                dr["updatetime"] = DateTime.Now.Date;
                dr["updateby"] = "";
                dt.Rows.Add (dr);
            }
            DataSetItem.Tables.Add (dt);
            return base.insert (TableName, DataSetItem);
        }

        bool IModisRep.update (long key, ModisModel itemModel) {
            DataSet DataSetItem = new DataSet ();
            DataTable dt = new DataTable ("MODIS_DATA_DIRECTORIES"); //创建一个名为Table的DataTalbe

            dt.Columns.Add (new DataColumn ("id", typeof (int))); //为dt_dry表内建立Column
            dt.Columns.Add (new DataColumn ("dataformat", typeof (string)));
            dt.Columns.Add (new DataColumn ("rowcol", typeof (string)));
            dt.Columns.Add (new DataColumn ("filetime", typeof (string)));
            dt.Columns.Add (new DataColumn ("year", typeof (int)));
            dt.Columns.Add (new DataColumn ("days", typeof (int)));
            dt.Columns.Add (new DataColumn ("filename", typeof (string)));
            dt.Columns.Add (new DataColumn ("mtype", typeof (string)));
            dt.Columns.Add (new DataColumn ("updatetime", typeof (DateTime)));
            dt.Columns.Add (new DataColumn ("updateby", typeof (string)));

            DataRow dr = dt.NewRow (); //注意这边创建dt的新行的方法。指定类型是DataRow而不是TableRow，然后不用new直接的用创建的DataTable下面的NewRow方法。
            dr["id"] = key;
            dr["dataformat"] = itemModel.dataformat;
            dr["rowcol"] = itemModel.rowcol;
            dr["filetime"] = itemModel.filetime;
            dr["year"] = itemModel.year;
            dr["days"] = itemModel.days;
            dr["filename"] = itemModel.filename;
            dr["mtype"] = itemModel.mtype;
            dr["updatetime"] = DateTime.Now.Date;
            dr["updateby"] = itemModel.updateby;
            dt.Rows.Add (dr);
            DataSetItem.Tables.Add (dt);
            return base.update ("id", "MODIS_DATA_DIRECTORIES", DataSetItem);
        }
    }
}